package cn.summit.validation.validor;


import cn.summit.validation.annotation.TextLength;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 字符串长度检查
 *
 * @author summit
 * @since 2020/6/24 11:19
 */
public class TextLengthValidator implements ConstraintValidator<TextLength, String> {

    private int min;

    private int max;

    @Override
    public void initialize(TextLength annotation) {
        this.min = annotation.min();
        this.max = annotation.max();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        int length = value.length();
        return length >= min && length <= max;
    }
}
